package com.qianfeng.es.service.impl;

//
//                            _ooOoo_
//                           o8888888o
//                           88" . "88
//                           (| -_- |)
//                            O\ = /O
//                        ____/`---'\____
//                      .   ' \\| |// `.
//                       / \\||| : |||// \
//                     / _||||| -:- |||||- \
//                       | | \\\ - /// | |
//                     | \_| ''\---/'' | |
//                      \ .-\__ `-` ___/-. /
//                   ___`. .' /--.--\ `. . __
//                ."" '< `.___\_<|>_/___.' >'"".
//               | | : `- \`.;`\ _ /`;.`/ - ` : | |
//                 \ \ `-. \_ __\ /__ _/ .-` / /
//         ======`-.____`-.___\_____/___.-`____.-'======
//                            `=---='
//
//         .............................................
//                  佛祖镇楼                  BUG辟易
//          佛曰:
//                  写字楼里写字间，写字间里程序员；
//                  程序人员写程序，又拿程序换酒钱。
//                  酒醒只在网上坐，酒醉还来网下眠；
//                  酒醉酒醒日复日，网上网下年复年。
//                  但愿老死电脑间，不愿鞠躬老板前；
//                  奔驰宝马贵者趣，公交自行程序员。
//                  别人笑我忒疯癫，我笑自己命太贱；
//


import com.qianfeng.es.service.AggsService;
import com.qianfeng.es.utils.ConnectionUtils;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.range.ParsedBinaryRange;
import org.elasticsearch.search.aggregations.bucket.range.ParsedRange;
import org.elasticsearch.search.aggregations.metrics.ParsedCardinality;
import org.elasticsearch.search.aggregations.metrics.ParsedExtendedStats;
import org.elasticsearch.search.builder.SearchSourceBuilder;

import java.io.IOException;

/**
 * Created by Jackiechan on 2022/10/13 15:58
 *
 * @author Jackiechan
 * @version 1.0
 * @since 1.0
 */
public class AggsServiceImpl implements AggsService {
    @Override
    public void cardinalityAggs(String index, String filed, String bieming) throws Exception {
//        RestHighLevelClient client = ConnectionUtils.getClient();
//        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
//        //设置聚合
//        searchSourceBuilder.aggregation(AggregationBuilders.cardinality(bieming).field(filed));
//        //创建查询的请求对象
//        SearchRequest request = new SearchRequest(index).source(searchSourceBuilder);
//        SearchResponse response = client.search(request, RequestOptions.DEFAULT);

        SearchResponse response = baseAggs(index, AggregationBuilders.cardinality(bieming).field(filed));
        //获取聚合的结果,注意实际返回的类型应该是一个子类型,但是不确定是什么类型,可以通过debug看看是什么类型
        ParsedCardinality aggregation = response.getAggregations().get(bieming);
        System.err.println(aggregation.getValue());
    }

    @Override
    public void rangeAggs(String index, String bieming, AggregationBuilder aggregationBuilder) throws Exception {
//        RestHighLevelClient client = ConnectionUtils.getClient();
//        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
//        //设置聚合
//        searchSourceBuilder.aggregation(aggregationBuilder);
//        //创建查询的请求对象
//        SearchRequest request = new SearchRequest(index).source(searchSourceBuilder);
//        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        SearchResponse response = baseAggs(index, aggregationBuilder);
        ParsedRange aggregations = response.getAggregations().get(bieming);
        aggregations.getBuckets().forEach(bucket->{
            Object key = bucket.getKey();
            long docCount = bucket.getDocCount();
            System.err.println("key--->" + key + "----doccount===>" + docCount);
        });
    }

    @Override
    public void IpAggs(String index, String bieming, AggregationBuilder aggregationBuilder) throws Exception {
        SearchResponse response = baseAggs(index, aggregationBuilder);
        //ip范围查询
        ParsedBinaryRange aggregations = response.getAggregations().get(bieming);
        aggregations.getBuckets().forEach(bucket->{
            Object key = bucket.getKey();
            long docCount = bucket.getDocCount();
            System.err.println("key--->" + key + "----doccount===>" + docCount);
        });
    }

    @Override
    public void extendedStatsAggs(String index, String filed, String bieming) throws Exception {
        SearchResponse response = baseAggs(index, AggregationBuilders.extendedStats(bieming).field(filed));
        ParsedExtendedStats aggregations = response.getAggregations().get(bieming);
        System.err.println(aggregations.getAvg());
        System.err.println(aggregations.getCount());
        System.err.println(aggregations.getMax());
    }


    public SearchResponse baseAggs(String index, AggregationBuilder aggregationBuilder) throws IOException {
        RestHighLevelClient client = ConnectionUtils.getClient();
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        //设置聚合
        searchSourceBuilder.aggregation(aggregationBuilder);
        //创建查询的请求对象
        SearchRequest request = new SearchRequest(index).source(searchSourceBuilder);
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        return response;
    }
}




















